﻿using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.And
{
    public class GatheringAppService : ApplicationService, IGatheringAppService
    {
        private readonly IRepository<ReceiptsAndPayments, int> _ReceiptsAndPaymentRepository;
        private readonly LogisticsDbContext _logisticsDbContext;

        public GatheringAppService(IRepository<ReceiptsAndPayments, int> ReceiptsAndPaymentRepository, LogisticsDbContext logisticsDbContext)
        {
            _logisticsDbContext = logisticsDbContext;
            _ReceiptsAndPaymentRepository = ReceiptsAndPaymentRepository;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Procurement"></param>
        /// <returns></returns>
        public async Task<GatheringDto> CreateAsync(GatheringDto Procurement)
        {
            var Statistics = new ReceiptsAndPayments
            {
                Numbers = "YS"+Convert.ToString(DateTime.Now),
                BusinessTIme = Procurement.BusinessTIme,
                Checker = 2,
                Handler = Procurement.Handler,
                SettlementMethod = Procurement.SettlementMethod,
                Outsourcing = Procurement.Outsourcing,
                Price = Procurement.Price,
                Remarks = Procurement.Remarks,
                State = false,
                SumPrice = Procurement.SumPrice,
                Tonnage = Procurement.Tonnage,
                CheckTime=DateTime.Now,
                CreateTime=DateTime.Now,
            };

            await _ReceiptsAndPaymentRepository.InsertAsync(Statistics);
            return ObjectMapper.Map<ReceiptsAndPayments, GatheringDto>(Statistics);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [RemoteService(false)]
        public async Task DeleteAsync(int id)
        {
            await _ReceiptsAndPaymentRepository.DeleteAsync(id);
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<GatheringDto> GetAsync(int id)
        {
            var food = await _ReceiptsAndPaymentRepository.GetAsync(id);
            return ObjectMapper.Map<ReceiptsAndPayments, GatheringDto>(food);
        }
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        public async Task<PagedResultDto<GatheringDto>> GetListAsync(GetGatheringListDto dto)
        {
            //var all = _ProcurementRepository.ToList();
            var all = _logisticsDbContext.ReceiptsAndPayments.ToList();
            int totoCount = all.Count();
            var data = new PagedResultDto<GatheringDto>();
            data.TotalCount = totoCount;
            all = all.Skip((dto.page - 1) * dto.limit).Take(dto.limit).ToList();
            data.Items = ObjectMapper.Map<List<ReceiptsAndPayments>, List<GatheringDto>>(all.ToList());
            return data;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="Gathering"></param>
        /// <returns></returns>

        public int UpdateAsync(GatheringDto Gathering)
        {
            var Payments = new ReceiptsAndPayments
            {
                Numbers = Gathering.Numbers,
                Id= Gathering.Id,
                BusinessTIme = Gathering.BusinessTIme,
                Checker = Gathering.Checker,
                Handler = Gathering.Handler,
                SettlementMethod= Gathering.SettlementMethod,
                Outsourcing= Gathering.Outsourcing,
                Price= Gathering.Price,
                Remarks= Gathering.Remarks,
                State= Gathering.State,
                SumPrice= Gathering.SumPrice,
                Tonnage= Gathering.Tonnage,
                CheckTime=DateTime.Now,
                CreateTime=DateTime.Now,
            };
            _logisticsDbContext.Entry(Payments).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            var i = _logisticsDbContext.SaveChanges();
            return i;
        }
    }
}
